//
//  BackPassWord.m
//  reSearchDemo
//
//  Created by 科技 on 15/7/31.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "BackPassWord.h"
#import "NewPassWord.h"
#import "HTTPGlobals.h"
@interface BackPassWord ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    UIView *_headView;
    UITextField *_userName_TF;
}
@end

@implementation BackPassWord

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor=[UIColor whiteColor];
//    [self setRightBarButtonImage:LOADIMAGE(@"btn_more") highlightedImage:nil selector:@selector(menu)];
    
    UIView * actionbar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
    self.navigationItem.titleView = actionbar;
    UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(8, 0, 100, 40)];
    lab.text=@"找回密码";
    lab.font=[UIFont systemFontOfSize:20];
    lab.textColor=[UIColor whiteColor];
    [actionbar addSubview:lab];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    _headView = [self buildMainView];
    
}
//初始化tableview头视图
-(UIView *)buildMainView {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    
    UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 80, 30)];
    userName.text = @"用户账号:";
    _userName_TF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(userName.frame), CGRectGetMinY(userName.frame), 140, CGRectGetHeight(userName.frame))];
    _userName_TF.text = @"15666666666";
    _userName_TF.textAlignment = NSTextAlignmentCenter;
    UIView *T_lane = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(_userName_TF.frame), CGRectGetMaxY(_userName_TF.frame) - 5, CGRectGetWidth(_userName_TF.frame), 1)];
    T_lane.backgroundColor = [UIColor blackColor];
    [headView addSubview:userName];
    [headView addSubview:_userName_TF];
    [headView addSubview:T_lane];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(userName.frame) + 10,100 ,30)];
    textLabel.text = @"安全问答:";
    [headView addSubview:textLabel];
    NSArray *titleArr = @[@"1.母亲叫什么名字?",@"2.父亲叫什么名字?",@"3.你最喜欢的运动项目是什么?"];
    for (int i = 0; i < titleArr.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) + i*50, 250, 30)];
        label.text = titleArr[i];
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame), 200, 20)];
        textField.tag = 1000+i;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(textField.frame), CGRectGetMaxY(textField.frame), 200, 1)];
        line.backgroundColor = [UIColor blackColor];
        [headView addSubview:label];
        [headView addSubview:textField];
        [headView addSubview:line];
    }
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) + 5*30 + 30, CGRectGetWidth(self.view.frame) - 2*CGRectGetMinX(textLabel.frame), 40);
    [sureBtn setTitle:@"确定" forState:0];
    [sureBtn setTitleColor:[UIColor blackColor] forState:0];
    [sureBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [sureBtn addTarget:self action:@selector(SureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:sureBtn];
    
    return headView;
}
//确认按钮
-(void)SureBtnAction:(UIButton *)sender {
    UITextField *TF_1 = (UITextField *)[self.view viewWithTag:1000];
    UITextField *TF_2 = (UITextField *)[self.view viewWithTag:1001];
    UITextField *TF_3 = (UITextField *)[self.view viewWithTag:1002];
    if (TF_1.text.length <= 0 || TF_2.text.length <= 0 || TF_3.text.length <= 0 || _userName_TF.text.length <= 0) {
        UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"注意" message:@"输入信息不能为空" delegate:nil cancelButtonTitle:@"退出" otherButtonTitles:nil, nil];
        [alert show];
    } else {
        NSString *userName = [NSString stringWithFormat:@"username=%@",_userName_TF.text];
        NSString *path1 = [NSString stringWithFormat:@"MobilePort/Index/safe.html?%@",userName];
        //获取用户密保数据和登录记录
        [[HTTPGlobals shareHTTPGlobal] GetHTTPRequest:path1 parameters:nil finish:^(BOOL seccess, NSString *error, id obj) {
            if (seccess) {
                DLog(@"获取密保数据: %@",obj);
                DLog(@"data = %@",obj[@"safedata"]);
                NSArray *titleArr = @[@"1.母亲叫什么名字?",@"2.父亲叫什么名字?",@"3.你最喜欢的运动项目是什么?"];
                NSMutableDictionary *state = [NSMutableDictionary dictionary];
                for (NSDictionary *dic in obj[@"safedata"]) {
                    if ([dic[@"question"] isEqualToString:titleArr[0]]) {
                        [state setObject:dic[@"safeid"] forKey:@"1"];
                    }else if ([dic[@"question"] isEqualToString:titleArr[1]]) {
                        [state setObject:dic[@"safeid"] forKey:@"2"];
                    }else if ([dic[@"question"] isEqualToString:titleArr[2]]) {
                        [state setObject:dic[@"safeid"] forKey:@"3"];
                    }
                }
                
                NSString *path = @"MobilePort/Index/check_user_public_safe.html";
                NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:state[@"1"],@"safeid",TF_1.text,@"answer", nil];
                NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:state[@"2"],@"safeid",TF_2.text,@"answer", nil];
                NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:state[@"3"],@"safeid",TF_3.text,@"answer", nil];
                NSArray *arr = [NSArray arrayWithObjects:dic1,dic2,dic3, nil];
                __block int code = 0;
                __block int number = 0;
                //验证用户密保问题
                for (int i = 0; i<arr.count; i++) {
                    [[HTTPGlobals shareHTTPGlobal] GetHTTPRequest:path parameters:arr[i] finish:^(BOOL seccess, NSString *error, id obj) {
                        if (seccess) {
                            DLog(@"用户密保设置 = %@",obj);
                            DLog(@"data = %@",obj[@"data"]);
                            if ([obj[@"code"] intValue] == 0) {
                                code ++;
                            }else {
                                code = 0;
                            }
                            number++;
                        }
                        if (number == arr.count) {
                            if (code < 3) {
                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"密保错误!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                [alert show];
                            }else {
                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"验证成功!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                [alert show];
                                [self pushViewController:[NewPassWord new]];
                            }
                        }
                        
                    }];
                }

            }
        }];
        
        
        
//        [self pushViewController:[NewPassWord new]];
    }
}

#pragma mark-
#pragma mark--tableviewDelegate--
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = _headView;
    return headView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    UIView *headView = [self tableView:tableView viewForHeaderInSection:section];
    
    return CGRectGetHeight(headView.frame);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
